package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"xiao-hong-book-mgr/cache"
	"xiao-hong-book-mgr/models"
	"xiao-hong-book-mgr/pkg"
)

func Login(c *gin.Context) {
	email := c.PostForm("email")
	password := c.PostForm("password")

	if email == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}

	var user models.User
	err := models.DB.Where("email=?", email).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}

	if pkg.Md5Str(password) != user.Password {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
	})
	return

}

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	img := c.PostForm("img")
	author := c.PostForm("author")

	if title == "" || content == "" || img == "" || author == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "内容不能为空",
		})
		return
	}

	redBook := models.RedBook{
		Title:   title,
		Content: content,
		Img:     img,
		Author:  author,
	}
	err := models.DB.Create(&redBook).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	redis_key := "redBook_id_" + strconv.Itoa(int(redBook.ID))
	var redBooks models.RedBook
	models.DB.Where("id=?", redBooks.ID).First(&redBooks)
	jsonRedBook, _ := json.Marshal(redBooks)
	cache.Rdb.Set(cache.Ctx, redis_key, jsonRedBook, time.Hour*2)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func Upload(c *gin.Context) {
	file, err := c.FormFile("img")
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "图片获取失败",
		})
		return
	}

	path := "static/img/" + file.Filename
	err = c.SaveUploadedFile(file, path)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "上传失败",
		})
		return
	} else {
		url := "http://" + c.Request.Host + "/" + path
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "上传成功",
			"data": url,
		})
		return
	}
}

func Detail(c *gin.Context) {
	id := c.Query("id")
	var redBook models.RedBook
	err := models.DB.Where("id=?", id).First(&redBook).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": redBook,
	})
	return
}

func Del(c *gin.Context) {
	id := c.Query("id")
	err := models.DB.Where("id=?", id).Delete(&models.RedBook{}).Error

	redis_key := "redBook_id_" + id
	cache.Rdb.Del(cache.Ctx, redis_key)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "删除失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
	return
}

func List(c *gin.Context) {
	var book models.RedBook
	redis_key := "news_list"
	cacheRedBook, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheRedBook == "" {
		models.DB.Find(&book)
		jsonRedBook, _ := json.Marshal(book)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonRedBook, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheRedBook), &book)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": book,
	})
	return
}
